package com.example.mapp;

import com.example.entity.ElevatorFaultReason;
import com.example.entity.EmergencyRescue;
import com.example.ext.entity.ExtEmergencyRescue;
import com.example.mapp.buildSql.EmergencyRescueMappSql;
import com.example.mapp.buildSql.SpotCheckPlanMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by youkun on 2017/10/3.
 */
@Mapper
public interface EmergencyRescueMapp {

    @Select(" select a.*, b.party_name as rescuePartyName from emergency_rescue a " +
            "left join party b on a.rescue_party_id=b.id where a.id=#{id}")
    ExtEmergencyRescue findById(String id) throws Exception;

    @Select(" select a.*, b.party_name as rescuePartyName from emergency_rescue a " +
            "left join party b on a.rescue_party_id=b.id where a.elevator_id=#{elevatorId} order by id desc")
    List<ExtEmergencyRescue> findList(ExtEmergencyRescue extEmergencyRescue) throws Exception;

    @Delete("delete from emergency_rescue where id=#{id}")
    int deleteById(String id)throws Exception;

    @Insert("insert into emergency_rescue " +
            "(elevator_id, elevator_num,received_time,arrived_time,rescue_end_time,pic_url,accident_reason,rescue_party_id,rescue_party_name," +
            "no_casualties,minor_wound_num,injured_num,death_num,protective,protective_oth," +
            "rescue_user_name1,rescue_user_name2,rescue_user_sign_url,rescue_user_sign_url2,signed_time,security_admin_sign_url,sured_time," +
            "human_reason_param,outside_reason_param,door_system_param,traction_system_param,guide_system_param,bridge_cabin_param,control_system_param,electric_system_param,safe_refinement_param," +
            "order_num,create_date,create_by,alarm_by,alarm_by_phone1,alarm_by_phone2,alarm_type,alarm_address,alarm_content,trapped_num," +
            "out_elevator_id,fault_category,fault_classification,order_status,order_process_status,remarks,status" +
            ") " +
            "value " +
            "(#{elevatorId},#{elevatorNum}, #{receivedTime},#{arrivedTime}, #{rescueEndTime},#{picUrl},#{accidentReason},#{rescuePartyId},#{rescuePartyName}," +
            "#{noCasualties},#{minorWoundNum},#{injuredNum},#{deathNum},#{protective},#{protectiveOth}" +
            ",#{rescueUserName1},#{rescueUserName2},#{rescueUserSignUrl},#{rescueUserSignUrl2},#{signedTime},#{securityAdminSignUrl},#{suredTime}," +
            "#{humanReasonParam},#{outsideReasonParam},#{doorSystemParam},#{tractionSystemParam},#{guideSystemParam},#{bridgeCabinParam},#{controlSystemParam},#{electricSystemParam},#{safeRefinementParam}," +
            "#{orderNum},#{createDate},#{createBy},#{alarmBy},#{alarmByPhone1},#{alarmByPhone2},#{alarmType}," +
            "#{alarmAddress},#{alarmContent},#{trappedNum},#{outElevatorId},#{faultCategory},#{faultClassification},#{orderStatus}," +
            "#{orderProcessStatus},#{remarks},#{status} )")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    int add(EmergencyRescue emergencyRescue) throws Exception;

    @Update("update emergency_rescue set elevator_id=#{elevatorId} where id=#{id}")
    int update(EmergencyRescue emergencyRescue) throws Exception;

    @Select("select type_en as typeEn ,type as type,fault_code as faultCode,fault_analysis as faultAnalysis from elevator_fault_reason ")
    List<ElevatorFaultReason> initFaultList();

    @SelectProvider(type = EmergencyRescueMappSql.class,method = "findlists")
    List<ExtEmergencyRescue> initWorkOrder(ExtEmergencyRescue extEmergencyRescue);

    // 更新工单
    @Update("update emergency_rescue set accident_reason=#{accidentReason},death_num=#{deathNum},elevator_id=#{elevatorId},elevator_num=#{elevatorNum}," +
            " injured_num=#{injuredNum},minor_wound_num=#{minorWoundNum},no_casualties=#{noCasualties}," +
            " protective=#{protective},pic_url=#{picUrl},protective_oth=#{protectiveOth}," +
            " received_time=#{receivedTime},rescue_end_time=#{rescueEndTime},rescue_party_id=#{rescuePartyId}," +
            " rescue_user_name1=#{rescueUserName1},rescue_user_name2=#{rescueUserName2},rescue_user_sign_url=#{rescueUserSignUrl},rescue_user_sign_url2=#{rescueUserSignUrl2}, " +
            " security_admin_sign_url=#{securityAdminSignUrl},signed_time=#{signedTime},sured_time=#{suredTime},arrived_time=#{arrivedTime}," +
            " rescue_party_name=#{rescuePartyName},signed_time2=#{signedTime2}," +
            " human_reason_param=#{humanReasonParam},outside_reason_param=#{outsideReasonParam}," +
            " door_system_param=#{doorSystemParam},traction_system_param=#{tractionSystemParam},guide_system_param=#{guideSystemParam}," +
            " bridge_cabin_param=#{bridgeCabinParam},control_system_param=#{controlSystemParam},electric_system_param=#{electricSystemParam}," +
            " safe_refinement_param=#{safeRefinementParam},status=#{status} where id=#{id}")
    void updateRescue(EmergencyRescue emergencyRescue);
}
